package Stream;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import static java.util.stream.Collectors.toList;
public class StreamAndIterator {

        public static void main(String[] args) {
            List<Dish> menu=Arrays.asList(
                    new Dish("pork",800),
                    new Dish("beef",700),
                    new Dish("chicken",400),
                    new Dish("french fries",530),
                    new Dish("rice",350),
                    new Dish("fruit",120),
                    new Dish("pizza",550));
            /*
             * 集合使用外部迭代
             */
            List<String> names=new ArrayList<>();
            Iterator<Dish> iterator=menu.iterator();
            while(iterator.hasNext()) {
                Dish d=iterator.next();
                names.add(d.getName());
            }
            System.out.println(names);

            /*
             * 流使用内部迭代
             */
            List<String> names2=menu.stream()
                    .map(Dish::getName)
                    .collect(toList());
            System.out.println(names2);

        }
    }

